package mt.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * @author nhp
 * @version Time of creating: 2013-7-25
 * @E-mail:nhp19850407@163.com
 */

public class DateTimeUtil {

	public static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");
	public static String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四",
		"星期五", "星期六" };

	public static String getYYMMDD(String dateTime){
		SimpleDateFormat lDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		
		SimpleDateFormat lDateFormat_1 = new SimpleDateFormat(
				"yyyyMMdd");
		Date date;
		try {
			date = lDateFormat.parse(dateTime);
			return lDateFormat_1.format(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	public static String getYYMMDD(Date date){
		
		SimpleDateFormat lDateFormat = new SimpleDateFormat(
				"yyyyMMdd");
		return lDateFormat.format(date);
	}
	public static String getCurrentDay(){
		Date nowDay = new Date();
		return dateFormat.format(nowDay);
	}
	/**
	 *把String转换为Date 格式为yyy-MM-dd 
	 * @param date  
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(String date) throws ParseException{
			return dateFormat.parse(date);
	}
	
	/**
	 * 把String转换为Date 格式为yyy-MM-dd  HH:mm:ss
	 * @param dateTime
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateTime(String dateTime) throws ParseException{
		SimpleDateFormat lDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		
		return lDateFormat.parse(dateTime);
	}
	
	/**
	 * 根据日期获得星期
	 * @param date
	 * @return
	 */
	public static String getWeekOfDate(Date date) {

		String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		return weekDaysCode[intWeek];
	}
	
	/**
	 * 根据日期获得星期
	 * @param date
	 * @return
	 * @throws ParseException 
	 */
	public static String getWeekOfDate(String dateStr) throws ParseException {
		Date date =getDate(dateStr);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		return weekDaysName[intWeek];
	}

	/**
	 * 获得周一到日期
	 * @param date
	 * @return
	 */
	public static String getMonday(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 获得周三到日期
	 * @param date
	 * @return
	 */
	public static String getWednesday(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 获得周五到日期
	 * @param date
	 * @return
	 */
	public static String getFriday(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 当前日期前几天或者后几天的日期
	 * @param n
	 * @return
	 */
	public static String afterNDay(int n) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, n);
		Date date = calendar.getTime();
		String s = dateFormat.format(date);
		return s;

	}

	/**
	 * 判断两个日期是否在同一周
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameWeekDates(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		if (0 == subYear) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
			// 濡傛灉12鏈堢殑鏈�悗涓�懆妯法鏉ュ勾绗竴鍛ㄧ殑璇濆垯鏈�悗涓�懆鍗崇畻鍋氭潵骞寸殑绗竴鍛�
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		return false;
	}

	/**
	 * 已知年份和月份，取得该月有多少天。
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getDaysOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		return days_of_month;
	}

	/**
	 * 取得两个日期之间的相差多少天
	 * @param date0
	 * @param date1
	 * @return
	 */
	public static long getDaysBetween(Date date0, Date date1) {
		long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000
																					// 鐢ㄧ珛鍗虫暟锛屽噺灏戜箻娉曡绠楃殑寮�攢
		return Math.abs(daysBetween);
	}
	/**
	 * 将Date 转为 XMLGregorianCalendar
	 * @param date
	 * @return
	 */
	public static XMLGregorianCalendar convertToXMLGregorianCalendar(Date date) {

        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        XMLGregorianCalendar gc = null;
        try {
            gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
        } catch (Exception e) {

             e.printStackTrace();
        }
        return gc;
    }
	
	/**
	 * 将String 转为 XMLGregorianCalendar
	 * @param dateTime
	 * @return
	 * @throws ParseException
	 */
	public static XMLGregorianCalendar convertToXMLGregorianCalendar(String dateTime) throws ParseException {
		return convertToXMLGregorianCalendar(getDateTime(dateTime));
	}
	
	/**
	 * 把XMLGregorianCalendar转换为Date
	 * @param cal
	 * @return
	 */
	public static Date ConvertToDate(XMLGregorianCalendar cal){
		if(cal == null ){
			return null;
		}
		GregorianCalendar ca = cal.toGregorianCalendar();

		if(ca == null ){
			return null;
		}
		System.out.println(ca.getTime());
		return ca.getTime();
	}
	
	public static void main(String[] args){
		String dateTime = "2013-4-1 15:40:15";
	
		try {
			System.out.println(DateTimeUtil.convertToXMLGregorianCalendar(dateTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
